/*
 * Copyright 2014 alexpad@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package alexpad.karaokarc.midi;

import java.io.File;
import javax.sound.midi.ShortMessage;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author alexpad@gmail.com
 */
public class MidiSongInfo implements java.io.Serializable, Comparable<MidiSongInfo>{
	
	private static final long serialVersionUID = 1L;
	
	public enum Status {
		NONE, MIDI_VALID, MIDI_INVALID
	}
	
	public enum Type {
		MID, KAR
	}
	
	// informazioni dal file
	private final String fileName;
	private final int fileSize;
	private final String fileHash;
	private final String artist;
	private final String title;
	private final Type type;
	
	// informazioni dalla sequence (post analisi)
	private String midiHash;
	private int duration;
	private int bpm;
	
	// status
	private Status status = Status.NONE;
	
	// voto utente
	private int score;
	
	@Override
	public int compareTo(MidiSongInfo that) {
		return fileName.compareTo(that.fileName);
	}
	
	public MidiSongInfo(File file) {
		fileName = file.getName();
		fileSize = (int) file.length();
		type = (FilenameUtils.getExtension(file.getName()).toLowerCase().equals("kar")?Type.KAR:Type.MID);
		fileHash = getFileHash(file);
		artist = getArtistName(file);
		title = getSongTitle(file);
	}
	
	public void populateSongInfo(MidiChannelMessages[] instruments, int duration, int bpm){
		this.midiHash = getMidiHash(instruments);
		this.duration = duration;
		this.bpm = bpm;
		this.status = Status.MIDI_VALID;
	}
	
	public void copySongInfo(MidiSongInfo that) {
		this.midiHash = that.midiHash;
		this.duration = that.duration;
		this.bpm = that.bpm;
		this.status = that.status;
	}
	
	public static String getFileHash(File file){
		return String.format("%X", (file.getName() + "_" + file.length() + "_" + file.lastModified()).hashCode());
	}
	
	public static String getArtistName(File file){
		String fileName = FilenameUtils.getBaseName(file.getName());
		int pos = fileName.lastIndexOf("-");
		if(pos>0){
			return fileName.substring(0, pos).trim();
		}
		throw new IllegalArgumentException("file '" + fileName + "' non valido: il nome non ha il formato %A - %T");
	}
	
	public static String getSongTitle(File file){
		String fileName = FilenameUtils.getBaseName(file.getName());
		int pos = fileName.lastIndexOf("-");
		if(pos>0){
			return fileName.substring(pos + 1).trim();
		}
		throw new IllegalArgumentException("file '" + fileName + "' non valido: il nome non ha il formato %A - %T");
	}
	
	public static String getMidiHash(MidiChannelMessages[] instruments) {
		StringBuffer sb = new StringBuffer(250);
		for(MidiChannelMessages item : instruments){
			if(item==null){
				sb.append("[]");
			}
			else{
				sb.append("[");
				for(ShortMessage msg : item.getMessages()){
					sb.append(String.format("%02X", msg.getData1())).append("-");
				}
				sb.setLength(sb.length()-1);
				sb.append("]");
			}
		}
		return sb.toString();
	}

	public String getFileName() {
		return fileName;
	}

	public String getFileHash() {
		return fileHash;
	}

	public String getMidiHash() {
		return midiHash;
	}

	public String getArtist() {
		return artist;
	}

	public String getTitle() {
		return title;
	}

	public int getScore() {
		return score;
	}

	public int getDuration() {
		return duration;
	}

	public int getBpm() {
		return bpm;
	}
	
	public int getFileSize() {
		return fileSize;
	}
	
	public String getFileSizeAsString() {
		return String.format("%.2f Kb", fileSize/1024.0F);
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}
	
	public Type getType() {
		return type;
	}
	
}
